package com.bw.Dao;

import com.bw.bean.Movie;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class MovieDaoImpl implements MovieDao{
    @Override
    public List<Movie> list() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
        String sql = "select * from movie";
        List<Movie> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Movie>(Movie.class));
        return query;
    }

    @Override
    public List<Movie> list2(String name) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
        String sql = "select * from movie where name like '%"+name+"%'";
        List<Movie> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Movie>(Movie.class));
        return query;
    }

    @Override
    public List<Movie> list3(String start, String end) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
        String sql = "select * from movie where uptime between '"+start+"' and '"+end+"'";
        List<Movie> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Movie>(Movie.class));
        return query;
    }

    @Override
    public int List4() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
        String sql = "select count(*) from movie group by area";
        int update = jdbcTemplate.update(sql, new BeanPropertyRowMapper<MovieDao>());
        return update;
    }
}
